package com.scraper;

import java.util.ArrayList;
import java.util.List;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * This class is responsible for retrieving pertinent data from web pages associated with calendars.
 * 
 * Example Code: 
 * 
 *      Scrape Calendar 
		String calendarURL = "http://www.uah.edu/registrar/calendars"; 
		CalendarWebScraper calendarScraper = new CalendarWebScraper();
		calendarScraper.setScraperType(ScraperType.Calendar);
		webReader.setScraper(calendarScraper);
		webReader.scrapeWebPage(calendarURL);
		CalendarWebData calendarData = calendarScraper.getScrapedData(); 
		
 * @author Micah Perry 
 */
public class CalendarWebScraper extends WebScraper{
	
	/**
	 * The data object for the current instance of the CourseListingWeScraper.
	 */
	protected CalendarWebData webData = new CalendarWebData(); 

	/**
	 * This method determines which scrape method to call depending on the current ScraperType.
	 * @param targetDocument The Document representing the HTML to be scraped.
	 */
	void scrapeWebPage(Document targetDocument) {
		scrapeCalendar(targetDocument);
	}
	
	/**
	 * This method scrapes the calendar webpage to retrieve pertinent data. 
	 * <p>
	 * In particular, it will return a collection of links representing the calendars for the current year. It also retrieves 
	 * the links representing the previous year, future calendars and archived calendars.
	 * <p>
	 * Example webpage: <a href="http://www.uah.edu/registrar/calendars">http://www.uah.edu/registrar/calendars</a>
	 * 
	 * @param calendarDocument The Document representing the HTML to be scraped. 
	 * @return List of current year calendar links.
	 */
	public List<Link> scrapeCalendar(Document calendarDocument){
		
		// Select the H2 headers, representing the type of calendars 
		String linkQuery = "h2";
		Elements courseLinks = calendarDocument.select(linkQuery);
		
		// Loop through each group of calendars 
		int calendarType = 1; 
		for(Element h2Element : courseLinks){
			List<Link> returnCurrentYearCalendarLinks = new ArrayList<Link>();
			
			// Select the anchors associated with the current H2 and loop through them to select the links to calendars 
			Elements aLinks = h2Element.nextElementSibling().select("a"); 
			for(Element aElement : aLinks){
				
				Link calendarLink = WebScraperCommon.constructLinkGivenTag(aElement); 
				
				if(!calendarLink.url.equals("") && !calendarLink.description.equals(""))
 					returnCurrentYearCalendarLinks.add(calendarLink); 
			} 
			
			saveCalendarLinks(returnCurrentYearCalendarLinks, calendarType);
			
			calendarType++; 
		}
		
		return webData.currentYearCalendars;
	}
	
	/**
	 * This method saves the retrieved calendar links in the appropriate variables of the WebData object.
	 * 
	 * @param calendarLinks
	 * @param calendarType
	 */
	private void saveCalendarLinks(List<Link> calendarLinks, int calendarType) {
		
		if(calendarType == 1)
		{
			webData.currentYearCalendars = calendarLinks; 
		}
		else if(calendarType == 2)
		{
			webData.previousYearCalendars = calendarLinks; 
		}
		else if(calendarType == 3)
		{
			webData.futureCalendars = calendarLinks; 
		}
		else if(calendarType == 4)
		{
			webData.archivedCalendars = calendarLinks; 
		}
	}
	
	/**
	 * This method returns the WebData object. 
	 * 
	 * @return The WebData object associated with the current instance of WebScraper.
	 */
	public CalendarWebData getScrapedData(){
		return this.webData;
	}

}
